<template>
  <div class="dialogContainer">
    <img class="avator" src="../assets/user-avator.png" />
    <div class="contentText" v-if="typeof props.content == 'string'">{{ props.content }}</div>
    <div v-else class="recorder">
      <div class="contentText" style="background: #5863ff; color: #fff; padding: 8px">
        <span @click="handleAudio">
          <!-- <img src="../assets/voice.png" /> -->
          <ul
            :class="{
              'wave-menu': true,
              start:
                audioState.status == RecorderTypeEnum.start ||
                props.content.state == RecorderTypeEnum.start
            }"
          >
            <li v-for="index in 10" :key="index"></li>
          </ul>
        </span>
        <span style="margin-left: 12px">{{ showText }}</span>
      </div>
      <div class="contentText" style="margin-top: 12px" v-if="props.content.text">
        {{ props.content.text }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { RecorderTypeEnum } from '../types.d'
import { handleSecondsToHms } from '../util'
import ChatAudioPlayer from '../ChatAudioPlayer.ts'

const props = defineProps({ content: String, index: Number })
const audioState = reactive({
  status: RecorderTypeEnum.end,
  currentTime: 0
})
const audioPlayer = ChatAudioPlayer.getInstance()

// 展示的文案
const showText = computed(() => {
  const _enum = {
    [RecorderTypeEnum.start]: '正在录音',
    [RecorderTypeEnum.pause]: '已暂停',
    [RecorderTypeEnum.end]: props.content.time
  }

  if (audioState.status == RecorderTypeEnum.start || audioState.status == RecorderTypeEnum.pause) {
    return handleSecondsToHms(audioState.currentTime)
  } else {
    return _enum[props.content.state]
  }
})

const handleAudio = () => {
  if (audioState.status == RecorderTypeEnum.start) {
    // 暂停播放
    audioPlayer.pause()
    audioState.status = RecorderTypeEnum.pause
  } else if (audioState.status == RecorderTypeEnum.pause) {
    // 重新播放
    audioPlayer.resume()
    audioState.status = RecorderTypeEnum.start
  } else {
    // 播放音频
    audioPlayer.play(props.content.src, {
      destroy: () => {
        audioState.status = RecorderTypeEnum.end
      },
      onProcess: (currentTime, duration) => {
        audioState.currentTime = Math.ceil(currentTime)
      }
    })
    audioState.status = RecorderTypeEnum.start
  }
}
</script>
<style lang="less" scoped>
.dialogContainer {
  display: flex;
  flex-direction: row-reverse;
  margin-bottom: 20px;

  .avator {
    width: 40px;
    height: 40px;
    margin-right: 12px;
    margin-left: 8px;
  }

  .contentText {
    display: flex;
    box-sizing: border-box;
    max-width: calc(100% - 60px);
    padding: 16px;
    border-radius: 8px;
    background: #e1e4ff;
  }

  .recorder {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
  }
}

/* From Uiverse.io by JkHuger */
.wave-menu {
  border-radius: 50px;
  height: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  margin: 0;
  cursor: pointer;
  transition: ease 0.2s;
  position: relative;
}

.wave-menu li {
  list-style: none;
  height: 24px;
  width: 4px;
  border-radius: 10px;
  background: #fff;
  margin: 0 6px;
  padding: 0;
  animation-duration: 0.3s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  transition: ease 0.2s;
  transform: scaleY(0.6);
}

.wave-menu:hover {
  background: #545fe5;
}

.wave-menu.start li:nth-child(1) {
  animation-name: wave1;
}

.wave-menu.start li:nth-child(2) {
  animation-name: wave2;
  animation-delay: 0.2s;
}

.wave-menu.start li:nth-child(3) {
  animation-name: wave3;
  animation-delay: 0.23s;
  animation-duration: 0.4s;
}

.wave-menu.start li:nth-child(4) {
  animation-name: wave4;
  animation-delay: 0.1s;
  animation-duration: 0.3s;
}

.wave-menu.start li:nth-child(5) {
  animation-name: wave1;
  animation-delay: 0.5s;
}

.wave-menu.start li:nth-child(6) {
  animation-name: wave2;
  animation-duration: 0.5s;
}

.wave-menu.start li:nth-child(7) {
  animation-name: wave1;
}

.wave-menu.start li:nth-child(8) {
  animation-name: wave4;
  animation-delay: 0.4s;
  animation-duration: 0.25s;
}

.wave-menu.start li:nth-child(9) {
  animation-name: wave3;
  animation-delay: 0.15s;
}

.wave-menu.start li:nth-child(10) {
  animation-name: wave1;
}

@keyframes wave1 {
  from {
    transform: scaleY(1);
  }

  to {
    transform: scaleY(0.5);
  }
}

@keyframes wave2 {
  from {
    transform: scaleY(0.3);
  }

  to {
    transform: scaleY(0.6);
  }
}

@keyframes wave3 {
  from {
    transform: scaleY(0.6);
  }

  to {
    transform: scaleY(0.8);
  }
}

@keyframes wave4 {
  from {
    transform: scaleY(0.2);
  }

  to {
    transform: scaleY(0.5);
  }
}
</style>
